package Socker;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * @Title: UDPClient
 * @Author 郑雨杭
 * @Package Socker
 * @Date 2024/7/10 15:21
 * @description: UDP客户端
 */
public class UDPClient {
    private DatagramSocket socket = null;
    private int path;
    private String ip;

    public UDPClient(String ip ,int path) throws SocketException {
        this.socket=new DatagramSocket();
        this.ip=ip;
        this.path=path;
    }
    protected void stact() throws IOException {
        System.out.println("客户端启动");
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.print("请输入：");
            String s = scanner.nextLine();
            DatagramPacket p = new DatagramPacket(s.getBytes(),s.getBytes().length, InetAddress.getByName(ip),path);
            socket.send(p);
            byte[] rem = new byte[4096];
            DatagramPacket ret = new DatagramPacket(rem,4096);
            socket.receive(ret);
            String resp = new String(ret.getData(),0, ret.getLength());
            System.out.println(resp);
        }
    }
    public static void main(String[] args) throws IOException {
        new UDPClient("127.0.0.1",9090).stact();
    }
}
